# -------------------------------- 基础配置 --------------------------------
# 动态获取项目名称（使用源码目录名）
get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(TOUPPER ${PROJECT_NAME} PROJECT_NAME_CAPITALIZED)

# -------------------------------- 版本管理 --------------------------------
# 使用 project() 内置版本管理
project(${PROJECT_NAME}
        VERSION 1.0.0
        LANGUAGES CXX
)

# 允许通过命令行覆盖版本参数
set(PROJECT_VERSION_MAJOR ${PROJECT_VERSION_MAJOR} CACHE STRING "主版本号")
set(PROJECT_VERSION_MINOR ${PROJECT_VERSION_MINOR} CACHE STRING "次版本号")
set(PROJECT_VERSION_PATCH ${PROJECT_VERSION_PATCH} CACHE STRING "补丁版本号")
set(VERSION_SUFFIX "" CACHE STRING "版本后缀 (如: beta, rc1)")

# 组合完整版本号
if (VERSION_SUFFIX)
    set(PROJECT_FULL_VERSION "${PROJECT_VERSION}-${VERSION_SUFFIX}")
else ()
    set(PROJECT_FULL_VERSION "${PROJECT_VERSION}")
endif ()

# -------------------------------- 构建信息 --------------------------------
set(CMAKE_MESSAGE_LOG_LEVEL NOTICE)
set(CMAKE_SUPPRESS_REGENERATION ON)
set(CMAKE_VERBOSE_MAKEFILE OFF)

# -------------------------------- Git 信息 --------------------------------
find_package(Git QUIET)
set(GIT_HASH "unknown")
set(GIT_BRANCH "unknown")

if (GIT_FOUND)
    execute_process(
            COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            OUTPUT_VARIABLE GIT_HASH_TMP
            OUTPUT_STRIP_TRAILING_WHITESPACE
            RESULT_VARIABLE GIT_RESULT
    )
    execute_process(
            COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            OUTPUT_VARIABLE GIT_BRANCH_TMP
            OUTPUT_STRIP_TRAILING_WHITESPACE
            RESULT_VARIABLE GIT_RESULT
    )

    if (GIT_RESULT EQUAL 0)
        set(GIT_HASH ${GIT_HASH_TMP})
        set(GIT_BRANCH ${GIT_BRANCH_TMP})
    endif ()
endif ()

string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%d %H:%M:%S")

# -------------------------------- Qt 6.8.3 配置 --------------------------------
# 设置 Qt 自动处理
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# 查找必需 Qt 组件
find_package(Qt6 REQUIRED COMPONENTS
        Core
        Gui
        Widgets
        Sql
)

# 设置 Qt 版本
set(QT_VERSION_MAJOR 6)

# -------------------------------- 编译选项 --------------------------------
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 创建 project_warnings 接口库（如果不存在）
if(NOT TARGET project_warnings)
    add_library(project_warnings INTERFACE)

    # 为不同编译器设置警告选项
    if (MSVC)
        target_compile_options(project_warnings INTERFACE
            /W4
            /WX
            /permissive-
        )
    elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
        target_compile_options(project_warnings INTERFACE
            -Wall
            -Wextra
            -Wpedantic
            -Werror
        )
    endif()
endif()

# -------------------------------- 平台特定配置 --------------------------------
if (WIN32)
    add_compile_definitions(
            WIN32_LEAN_AND_MEAN
            _WIN32_WINNT=0x0A00
    )
elseif (APPLE)
    add_compile_definitions(MACOS)
    set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
else ()
    add_compile_definitions(LINUX)
endif ()

# -------------------------------- 安装规则 --------------------------------
include(GNUInstallDirs)

# 安装目标函数
function(install_target TARGET_NAME)
    install(TARGETS ${TARGET_NAME}
            BUNDLE DESTINATION .
            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    )

    if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/assets")
        install(DIRECTORY assets/
                DESTINATION ${CMAKE_INSTALL_DATADIR}/${TARGET_NAME}
        )
    endif ()

    if (APPLE)
        install(CODE "include(BundleUtilities)")
        install(CODE "fixup_bundle(\${CMAKE_INSTALL_PREFIX}/${TARGET_NAME}.app \"\" \"\")")
    endif ()
endfunction()

# 创建项目目录结构函数
function(create_project_structure)
    set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
    set(SUBDIRS
            "${SRC_DIR}/resource"
            "${SRC_DIR}/utils"
            "${SRC_DIR}/widget"
    )

    foreach (dir ${SUBDIRS})
        if (NOT EXISTS ${dir})
            file(MAKE_DIRECTORY ${dir})
        endif ()
    endforeach ()
endfunction() 